/* SetupRanking page controller */
(function () {
    'use strict';
    angular.module('module.business')
        .controller('CertificateAwardController', CertificateAwardController);

    CertificateAwardController.$inject = ['$rootScope', '$scope', '$http', '$state', '$uibModal', '$uibModalInstance', '$log', '$document', '$uibPosition', '$interval', '$q', 'recordNo'];

    function CertificateAwardController($rootScope, $scope, $http, $state, $uibModal, $uibModalInstance, $log, $document, $uibPosition, $interval, $q, recordNo) {
        var vm = this;

        vm.activate = activate;
        vm.retrieveRecordsDetail = retrieveRecordsDetail;
        vm.preGenerateCertificateNo = preGenerateCertificateNo;
        vm.exportCertificate = exportCertificate;

        vm.ok = ok;
        vm.cancel = cancel;
        vm.recordInfo = {};
        vm.certificateNo = '';

        //页面初始化
        activate();

        return vm;
        
        function retrieveRecordsDetail() {
            $http.get('app/module/business/data/records.json').then(function(data){
                console.log(data.data.record);
                data.data.record.forEach(function (e) {
                    if(recordNo === e.RECORD_NO){
                        vm.recordInfo = e;
                    }
                });
            },function(data){

            });

        }

        function preGenerateCertificateNo() {
            var modalInstance = $uibModal.open({
                animation: true,
                ariaLabelledBy: 'modal-title',
                ariaDescribedBy: 'modal-body',
                templateUrl: 'certificateGenerate.html',
                controller: 'CertificateGenerateController',
                controllerAs: 'certificateGenerate',
                size: 'md'
            });
            modalInstance.result.then(function (certificateNoGenerated) {
                vm.certificateNo = certificateNoGenerated;
                //doSomething
            }, function () {
                //doSomething
            });
        }

        function exportCertificate() {
            var urlCreator = window.URL || window.webkitURL || window.mozURL || window.msURL;
            if(urlCreator) {
                // Try to use a download link
                var link = document.createElement('a');
                if ('download' in link) {
                    // Try to simulate a click
                    try {
                        // Prepare a blob URL
                        console.log('Trying download link method with simulated click ...');
                        var blob = new Blob(['存放到本地测试'],{type:'text/plain'});
                        console.log(blob);
                        var url = urlCreator.createObjectURL(blob);
                        link.setAttribute('href', url);
                        // Set the download attribute (Supported in Chrome 14+ / Firefox 20+)
                        link.setAttribute('download', 'test.txt');
                        // Simulate clicking the download link
                        var event = document.createEvent('MouseEvents');
                        event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
                        link.dispatchEvent(event);
                        console.log('Download link method with simulated click succeeded');

                    } catch (ex) {
                        console.log('Download link method with simulated click failed with the following exception:');
                        console.log(ex);
                    }
                }
            }

        }

        function ok() {
            $uibModalInstance.close();
        }

        function cancel() {
            $uibModalInstance.dismiss('cancel');
        }

        /**
         * @description     页面初始化
         */
        function activate() {
            $scope.$on('$viewContentLoaded', function () {
                App.initAjax(); // initialize core components
            });

            $rootScope.settings.layout.pageContentWhite = true;
            $rootScope.settings.layout.pageBodySolid = true;
            $rootScope.settings.layout.pageSidebarClosed = false;

            retrieveRecordsDetail();
        }
    }
})();
